//
//  MYGCloudDiskEditFileSheet.swift
//  MYDoctor
//
//  Created by Denny on 2024/3/11.
//

import UIKit
import HandyJSON
class MYGCloudDiskEditFileSheet: UIView {
    @IBOutlet weak var leftMrgin4: NSLayoutConstraint!
    
    @IBOutlet weak var timeLB: UILabel!
    @IBOutlet weak var titleLB: UILabel!
    @IBOutlet weak var leftMargin3: NSLayoutConstraint!
    @IBOutlet weak var leftMargin2: NSLayoutConstraint!
    @IBOutlet weak var leftMargin1: NSLayoutConstraint!
    var tapCloseCompletion:(()->Void)?
    var tapRenameCompletion:(()->Void)?
    var tapShareCompletion:(()->Void)?
    var fileModel = MYCloudDiskContentModel()

    override  func awakeFromNib() {
        let margin = max(0, (screenWidth - 70 * 4)/5)
        leftMargin1.constant = margin
        leftMargin2.constant = margin
        leftMargin3.constant = margin
        leftMrgin4.constant = margin
    }
    
    func bindData(_ data: MYCloudDiskContentModel) {
        self.titleLB.text = data.name
        self.timeLB.text = data.created_at
    }
    
     @IBAction func tapClose(_ sender: UIButton) {
        if let con = tapCloseCompletion {
            con()
        }
    }
    @IBAction func tapDownload(_ sender: UIButton) {
        MYGLoading.show()
        //查看文件夹所有可以下载的url
        DennyNetwork.request(MYGMeAPI.netdiskDownload(param: ["f_id":fileModel.id]), modelType: [MYCloudDiskContentModel].self) { result in
            MYGLoading.hide()
            MYGDownloadManager.shared.addDownloadTasks(result)
        } failed: { code, msg in
            MMYGHUD.showToast(text: msg)
            MYGLoading.hide()
        }
    }
    
    @IBAction func tapShare(_ sender: UIButton) {
        if let con = tapShareCompletion {
            con()
        }
    }
    @IBAction func tapRename(_ sender: UIButton) {
        if let con = tapRenameCompletion {
            con()
        }
    }
    @IBAction func tapDelete(_ sender: UIButton) {
        if let con = tapCloseCompletion {
            con()
        }
        MYGLoading.show()
        MYGNetwork<MYGMeAPI,MYGEmptyModel>.request(target: .deleteNetDiskFolder(id: fileModel.id)) { item in
            NotificationCenter.default.post(name: NSNotification.Name("MYCloudDiskViewController_configDataSource"), object: nil)
            MYGLoading.hide()
        } failed: { _, error in
            MYGLoading.hide()
        }
    }
    
}

 
struct MYGDownloadUrlModel:HandyJSON {
    var name = ""
    var url = ""
}
